import { get, post, put, del } from '@/utils/request'

// 获取营业状态
export function getBusinessStatus() {
  return get('/business/status')
}

// 更新营业状态
export function updateBusinessStatus(status) {
  return post('/business/status', { status }, {
    showLoading: true,
    loadingText: '更新营业状态...'
  })
}

// 获取营业时间设置
export function getBusinessHours() {
  return get('/business/hours')
}

// 更新营业时间
export function updateBusinessHours(hours) {
  return put('/business/hours', hours, {
    showLoading: true,
    loadingText: '更新营业时间...'
  })
}

// 获取服务表现数据
export function getPerformanceData() {
  return get('/business/performance')
}

// 获取账户余额
export function getAccountBalance() {
  return get('/business/balance')
}

// 账户充值
export function rechargeAccount(data) {
  return post('/business/recharge', data, {
    showLoading: true,
    loadingText: '处理充值中...'
  })
}

// 获取交易明细
export function getTransactionHistory(params = {}) {
  return get('/business/transactions', params)
}

// 获取推广统计数据
export function getPromotionStats() {
  return get('/business/promotion/stats')
}

// 获取推广方案列表
export function getPromotionPlans() {
  return get('/business/promotion/plans')
}

// 购买推广方案
export function purchasePromotion(planId, data = {}) {
  return post(`/business/promotion/purchase/${planId}`, data, {
    showLoading: true,
    loadingText: '购买推广中...'
  })
}

// 停止推广
export function stopPromotion(planId) {
  return post(`/business/promotion/stop/${planId}`)
}

// 切换自动调节
export function toggleAutoAdjustment(enabled) {
  return post('/business/promotion/auto-adjustment', { enabled })
}

// 获取风控提醒
export function getRiskAlerts() {
  return get('/business/risk/alerts')
}

// 标记风控提醒已读
export function markRiskAlertRead(alertId) {
  return post(`/business/risk/alerts/${alertId}/read`)
}

// 获取营业设置
export function getBusinessSettings() {
  return get('/business/settings')
}

// 更新营业设置
export function updateBusinessSettings(settings) {
  return put('/business/settings', settings, {
    showLoading: true,
    loadingText: '更新设置中...'
  })
}

// 获取服务区域设置
export function getServiceAreas() {
  return get('/business/service-areas')
}

// 更新服务区域
export function updateServiceAreas(areas) {
  return put('/business/service-areas', areas, {
    showLoading: true,
    loadingText: '更新服务区域...'
  })
}

// 获取配送设置
export function getDeliverySettings() {
  return get('/business/delivery-settings')
}

// 更新配送设置
export function updateDeliverySettings(settings) {
  return put('/business/delivery-settings', settings, {
    showLoading: true,
    loadingText: '更新配送设置...'
  })
}

// 获取自动接单设置
export function getAutoAcceptSettings() {
  return get('/business/auto-accept-settings')
}

// 更新自动接单设置
export function updateAutoAcceptSettings(settings) {
  return put('/business/auto-accept-settings', settings)
}

// 获取推广效果分析
export function getPromotionAnalysis(timeRange = '7d') {
  return get('/business/promotion/analysis', { timeRange })
}

// 获取营业数据分析
export function getBusinessAnalysis(params = {}) {
  return get('/business/analysis', params)
}

// 获取竞争对手分析
export function getCompetitorAnalysis() {
  return get('/business/competitor-analysis')
}

// 获取营业建议
export function getBusinessSuggestions() {
  return get('/business/suggestions')
}

// 申请高级推广
export function applyAdvancedPromotion(data) {
  return post('/business/promotion/apply-advanced', data, {
    showLoading: true,
    loadingText: '提交申请中...'
  })
}

// 获取推广申请状态
export function getPromotionApplicationStatus() {
  return get('/business/promotion/application-status')
}

// 获取营业日志
export function getBusinessLogs(params = {}) {
  return get('/business/logs', params)
}

// 导出营业数据
export function exportBusinessData(params = {}) {
  return get('/business/export', params, {
    responseType: 'blob'
  })
}

// 营业状态常量
export const BUSINESS_STATUS = {
  OPEN: 'OPEN',           // 营业中
  CLOSED: 'CLOSED',       // 已打烊
  BREAK: 'BREAK',         // 休息中
  MAINTENANCE: 'MAINTENANCE' // 维护中
}

export const BUSINESS_STATUS_TEXT = {
  [BUSINESS_STATUS.OPEN]: '营业中',
  [BUSINESS_STATUS.CLOSED]: '已打烊',
  [BUSINESS_STATUS.BREAK]: '休息中',
  [BUSINESS_STATUS.MAINTENANCE]: '维护中'
}

export const BUSINESS_STATUS_COLOR = {
  [BUSINESS_STATUS.OPEN]: 'success',
  [BUSINESS_STATUS.CLOSED]: 'danger',
  [BUSINESS_STATUS.BREAK]: 'warning',
  [BUSINESS_STATUS.MAINTENANCE]: 'info'
}

// 推广等级常量
export const PROMOTION_LEVELS = {
  LOW: 'low',
  MEDIUM: 'medium',
  HIGH: 'high',
  SUPER: 'super'
}

export const PROMOTION_LEVEL_TEXT = {
  [PROMOTION_LEVELS.LOW]: '低曝光',
  [PROMOTION_LEVELS.MEDIUM]: '中等曝光',
  [PROMOTION_LEVELS.HIGH]: '高曝光',
  [PROMOTION_LEVELS.SUPER]: '超级曝光'
}

export const PROMOTION_LEVEL_COLOR = {
  [PROMOTION_LEVELS.LOW]: 'info',
  [PROMOTION_LEVELS.MEDIUM]: 'warning',
  [PROMOTION_LEVELS.HIGH]: 'danger',
  [PROMOTION_LEVELS.SUPER]: 'success'
}

// 风控等级常量
export const RISK_LEVELS = {
  LOW: 'low',
  MEDIUM: 'medium',
  HIGH: 'high',
  CRITICAL: 'critical'
}

export const RISK_LEVEL_TEXT = {
  [RISK_LEVELS.LOW]: '低风险',
  [RISK_LEVELS.MEDIUM]: '中风险',
  [RISK_LEVELS.HIGH]: '高风险',
  [RISK_LEVELS.CRITICAL]: '严重风险'
}

export const RISK_LEVEL_COLOR = {
  [RISK_LEVELS.LOW]: 'success',
  [RISK_LEVELS.MEDIUM]: 'warning',
  [RISK_LEVELS.HIGH]: 'danger',
  [RISK_LEVELS.CRITICAL]: 'danger'
}

// 支付方式常量
export const PAYMENT_METHODS = {
  WECHAT: 'wechat',
  ALIPAY: 'alipay',
  BANK: 'bank'
}

export const PAYMENT_METHOD_TEXT = {
  [PAYMENT_METHODS.WECHAT]: '微信支付',
  [PAYMENT_METHODS.ALIPAY]: '支付宝',
  [PAYMENT_METHODS.BANK]: '银行卡'
}

// 推广方案类型
export const PROMOTION_PLAN_TYPES = {
  SMART: 'smart',           // 智能推广
  NEWBIE: 'newbie',         // 新品推广
  ADVANCED: 'advanced'      // 高级推广
}

export const PROMOTION_PLAN_TYPE_TEXT = {
  [PROMOTION_PLAN_TYPES.SMART]: '智能推广',
  [PROMOTION_PLAN_TYPES.NEWBIE]: '新品极速推广',
  [PROMOTION_PLAN_TYPES.ADVANCED]: '高级智能推广'
}

// 获取推广等级类型
export function getPromotionLevelType(level) {
  return PROMOTION_LEVEL_COLOR[level] || 'info'
}

// 获取推广等级文本
export function getPromotionLevelText(level) {
  return PROMOTION_LEVEL_TEXT[level] || '未知'
}

// 获取风控等级类型
export function getRiskLevelType(level) {
  return RISK_LEVEL_COLOR[level] || 'info'
}

// 获取风控等级文本
export function getRiskLevelText(level) {
  return RISK_LEVEL_TEXT[level] || '未知'
}

// 计算服务评分等级
export function getScoreLevel(score) {
  if (score >= 4.5) return 'excellent'
  if (score >= 4.0) return 'good'
  if (score >= 3.5) return 'average'
  return 'poor'
}

// 计算响应时间等级
export function getResponseTimeLevel(time) {
  if (time <= 2) return 'excellent'
  if (time <= 5) return 'good'
  if (time <= 10) return 'average'
  return 'poor'
}

// 计算退款率等级
export function getRefundRateLevel(rate) {
  if (rate <= 1) return 'excellent'
  if (rate <= 3) return 'good'
  if (rate <= 5) return 'average'
  return 'poor'
}
